//
//  ImageViewController.m
//  JiuLe
//
//  Created by SunChao on 16/12/1.
//  Copyright © 2016年 Fine. All rights reserved.
//
#define MaxSCale 3.0  //最大缩放比例
#define MinScale 1.0  //最小缩放比例
#define PHOTONUMBERS self.imgArray.count  //照片数量
#define _kScreenWidth [UIScreen mainScreen].bounds.size.width
#define _kScreenHeight [UIScreen mainScreen].bounds.size.height

#import "ImageViewController.h"
#import "UIImageView+WebCache.h"
@interface ImageViewController ()<UIScrollViewDelegate>
{
    float offset;
    float y;
}
@property (nonatomic, retain) UIScrollView *imageScrollView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation ImageViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    offset = 0.0;
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
    [self.view addGestureRecognizer:tapGestureRecognizer];
    self.view.backgroundColor = [UIColor blackColor];
    self.imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _kScreenWidth, _kScreenHeight)];
    self.imageScrollView.backgroundColor = [UIColor clearColor];
    self.imageScrollView.scrollEnabled = YES;
    self.imageScrollView.pagingEnabled = YES;
    self.imageScrollView.delegate = self;
    self.imageScrollView.contentSize = CGSizeMake(_kScreenWidth*PHOTONUMBERS, _kScreenHeight);
    self.imageScrollView.contentOffset = CGPointMake(_kScreenWidth*_imgIndex, 0);
    self.dataArray = [NSMutableArray new];
    for (int i = 0; i<PHOTONUMBERS; i++){
        UIScrollView *s = [[UIScrollView alloc] initWithFrame:CGRectMake(_kScreenWidth*i, 0, _kScreenWidth, _kScreenHeight)];
        s.backgroundColor = [UIColor clearColor];
        s.contentSize = CGSizeMake(_kScreenWidth, _kScreenHeight);
        s.showsHorizontalScrollIndicator = NO;
        s.showsVerticalScrollIndicator = NO;
        s.delegate = self;
        s.minimumZoomScale = MinScale;
        s.maximumZoomScale = MaxSCale;
        s.tag = i+100;
        [s setZoomScale:1.0];
        NSString *imgName = [self.imgArray objectAtIndex:i];
        float imageH = [self gainImageHeightWithString:[self.imgArray objectAtIndex:i]];
        UIImageView *imageview = [[UIImageView alloc] init];
        [imageview sd_setImageWithURL:[NSURL URLWithString:imgName] placeholderImage:[UIImage imageNamed:@"商城首页默认图片"]];
        y = ([UIScreen mainScreen].bounds.size.height - imageH)/2;
        imageview.frame = CGRectMake(0, y , _kScreenWidth, imageH);
        imageview.tag = i+1;
        [s addSubview:imageview];
        [self.imageScrollView addSubview:s];
    }
    
    [self.view addSubview:self.imageScrollView];
}

- (float)gainImageHeightWithString:(NSString  *)string
{
    NSArray *tmpArr1 = [string componentsSeparatedByString:@"_size_"];
    NSString *str2 =  [tmpArr1 objectAtIndex:1];
    NSArray *tmpArr2 = [str2 componentsSeparatedByString:@"."];
    NSString *str3 = [tmpArr2 objectAtIndex:0];
    NSArray *imgArr = [str3 componentsSeparatedByString:@"*"];
    float w = [[imgArr objectAtIndex:0]floatValue];
    float h = [[imgArr objectAtIndex:1]floatValue];
    float rate = w/_kScreenWidth;
    float imageH = h/rate;
    [self.dataArray addObject:[NSNumber numberWithFloat:imageH]];
    return imageH;
}

#pragma mark - ScrollView delegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    for (UIView *v in scrollView.subviews){
        return v;
    }
    return nil;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView == self.imageScrollView){
        CGFloat x = scrollView.contentOffset.x;
        if (x==offset){
        }
        else {
            offset = x;
            for (UIScrollView *s in scrollView.subviews){
                if ([s isKindOfClass:[UIScrollView class]]){
                    [s setZoomScale:1.0];
                    UIImageView *image = [[s subviews] objectAtIndex:0];
                    float imgHeight = [[self.dataArray objectAtIndex:(image.tag-1)]floatValue];
                    y = (_kScreenHeight - imgHeight)/2;
                    image.frame = CGRectMake(0, y , _kScreenWidth, imgHeight);
                }
            }
        }
    }
}
//让图片保持居中
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
     UIView *v = [scrollView.subviews objectAtIndex:0];
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
    v.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}
//回到商品详情页面
-(void)hideImageView:(UITapGestureRecognizer *)tap{
    [self dismissViewControllerAnimated:NO completion:nil];
}
    
@end
